//
//  ViewController.m
//  UI_19_JSON网易新闻
//
//  Created by li cheng on 16/8/9.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "NewsTitle.h"
#import "MyCollectionViewLayOut.h"
#import "CollectionViewCell.h"
#import "LCCollectionViewLayout.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LCCollectionViewLayoutDelegate>
@property (nonatomic, strong) NSMutableArray<NewsTitle *> *newsTitleArray;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    LCCollectionViewLayout *layout = (LCCollectionViewLayout *)self.myCollectionView.collectionViewLayout;
    layout.delegate = self;
}
- (void)handleData{
    self.newsTitleArray = [NSMutableArray array];
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    NSError *error = nil;
    NSDictionary *newsTitleDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSArray *tList = [newsTitleDic objectForKey:@"tList"];
    for (NSDictionary *newsTitle in tList) {
        NewsTitle *news = [NewsTitle newsTitleWithDic:newsTitle];
        [_newsTitleArray addObject:news];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _newsTitleArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.myLabel.text = _newsTitleArray[indexPath.row].tname;
    return cell;
}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath{
////    return 100;
//    NewsTitle *newsTitle = _newsTitleArray[indexPath.row];
//    CGRect newRect = [newsTitle.tname boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,CGRectGetHeight( self.myCollectionView.bounds)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:17]} context:nil];
//    
//    CGFloat width = ceilf(CGRectGetWidth(newRect));
//    
//    return width;
//}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
    NewsTitle *newsTitle = _newsTitleArray[indexPath.row];
    CGRect newRect = [newsTitle.tname boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,CGRectGetHeight( self.myCollectionView.bounds)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:17]} context:nil];
    
    CGFloat width = ceilf(CGRectGetWidth(newRect));
    return width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
